package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-20
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {
    @Autowired
    MediaFeign mediaFeign;
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    IApUserService userService;

    /**
     * 用户认证列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult listByStatus(AuthDto dto) {
        //如果没有传状态,查询所有,传了状态,根据状态查询
        LambdaQueryWrapper<ApUserRealname> query = new LambdaQueryWrapper<>();
        //传了状态
        if (dto.getStatus() != null) {
            query.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        IPage<ApUserRealname> iPage = this.page(page, query);
        //创建返回对象
        PageResponseResult<Object> result = new PageResponseResult<>();
        //填充返回数据
        result.setTotal(iPage.getTotal());
        result.setData(iPage.getRecords());
        result.setCurrentPage(dto.getPage());
        result.setSize(dto.getSize());
        //返回给前端
        return result;
    }

    /**
     * 用户认证驳回
     *
     * @param dto
     * @param type  0为驳回  1为通过
     * @return
     */
    @GlobalTransactional //开启全局事务
    @Override
    public ResponseResult authFailOrPass(AuthDto dto, int type) {
        //根据申请用户id查询用户的实名信息
        ApUserRealname realName = this.getById(dto.getId());

        //若为0,驳回
        if (type == 0) {
            //更改实名用户的状态为2 驳回
            realName.setStatus(2);
            //更改数据库状态数据
            this.updateById(realName);
            return ResponseResult.okResult();
        }
        // 审核通过
        // 查询用户注册表信息
        ApUser apUser = userService.getById(realName.getUserId());
        //1.创建自媒体账号
        WmUser wmUser = createWmUser(apUser);
        if (wmUser != null) {
            //2.创建作者
            ApAuthor apAuthor = createApAuthor(wmUser);
            if (apAuthor != null){
                //修改用户认证表的状态为9
                realName.setStatus(9);
                this.updateById(realName);
                //修改用户注册表的flag为1 (自媒体人)
                apUser.setFlag(1);
                userService.updateById(apUser);
                //将作者id填充到自媒体用户表里
                wmUser.setApAuthorId(apAuthor.getId());
                //远程调用填充作者id值
                mediaFeign.updateWmUser(wmUser);

            }
        }
        //模拟报错
        //int a = 1/0;
        return ResponseResult.okResult();


    }

    /**
     * 新增作者
     *
     * @param wmUser
     * @return
     */
    private ApAuthor createApAuthor(WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        //封装数据
        BeanUtils.copyProperties(wmUser, apAuthor);
        apAuthor.setId(null);
        apAuthor.setUserId(wmUser.getApUserId());
        apAuthor.setType(2);
        apAuthor.setWmUserId(wmUser.getId());
        apAuthor.setCreatedTime(new Date());
        //远程调用
        ResponseResult<ApAuthor> apAuthorResponseResult = articleFeign.saveApAuthor(apAuthor);
        if (apAuthorResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            ApAuthor author = apAuthorResponseResult.getData();
            return author;
        }
        return null;
    }

    /**
     * 新增自媒体账户
     *
     * @param apUser
     * @return
     */
    public WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = new WmUser();
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setStatus(9);
        wmUser.setNickname(apUser.getName());
        wmUser.setCreatedTime(new Date());
        //发起远程调用
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);
        if (wmUserResponseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser data = wmUserResponseResult.getData();
            return data;
        }
        return null;
    }
}
